package product;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class MemberInput extends JInternalFrame {
	ProductMain pm;
	MemberUpdate mupdate;

	Connection conn;
	PreparedStatement pstmt = null;
	MemberSelect ms;
	ResultSet rs;
	
	HashMap<String, MemberData> mam;// 사원정보 맵
	MemberData mdata;// 사원정보 데이터

	// 해쉬맵 저장,불러오기용
	ObjectOutputStream oos = null;
	ObjectInputStream ois = null;

	// 사원코드번호 저장,불러오기용
	DataOutputStream dos = null;
	DataInputStream dis = null;

	int memberNum = 1;// 사원번호 카운트

	String pos;

	String str[] = { "사원", "대리", "과장", "부장" };// 콤보박스 하기 위함
	String str2[] = { "재경부", "기획부", "홍보부", "개발부", "마케팅부", "총무부" };// 부서 콤보박스 배열

	String combo = "사원";
	String team2 = "재경부";// 부서 콤보박스

	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	public JTextField code;
	private JTextField irum;
	private JTextField jumin;
	private JTextField address;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JLabel label_8;
	private JLabel label_9;
	private JTextField phone;
	private JTextField email;
	private JTextField pwd;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JComboBox position;
	private JScrollPane scrollPane_1;
	private JComboBox team;

	public MemberInput(Connection conn, ProductMain p) {
		super("MemberInput", true, true, true, false);
		this.conn=conn;
		this.pm = p;

		setBounds(450, 200, 400, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getCode());
		getContentPane().add(getIrum());
		getContentPane().add(getJumin());
		getContentPane().add(getAddress());
		getContentPane().add(getLabel_5());
		getContentPane().add(getLabel_6());
		getContentPane().add(getLabel_7());
		getContentPane().add(getLabel_8());
		getContentPane().add(getLabel_9());
		getContentPane().add(getPhone());
		getContentPane().add(getEmail());
		getContentPane().add(getPwd());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());
		getContentPane().add(getScrollPane_1());
		
		
//		String sql = "select * from memberdata";
//		pstmt = conn.prepareStatement(sql);
//		pstmt.setString(1, c);
//		rs = pstmt.executeQuery();
//
//		while (rs.next()) {


	}
	public void setD(MemberSelect ms){
		this.ms=ms;
	}
	

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사원번호");
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 13));
			lblNewLabel.setBounds(12, 10, 81, 23);
		}
		return lblNewLabel;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("성       명");
			label.setFont(new Font("굴림", Font.PLAIN, 13));
			label.setBounds(12, 43, 81, 23);
		}
		return label;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("주민번호");
			label_1.setFont(new Font("굴림", Font.PLAIN, 13));
			label_1.setBounds(12, 76, 81, 23);
		}
		return label_1;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("거  주  지");
			label_2.setFont(new Font("굴림", Font.PLAIN, 13));
			label_2.setBounds(12, 109, 81, 23);
		}
		return label_2;
	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();

			code.setText("A1");
			code.setBounds(81, 10, 109, 23);
			code.setColumns(10);
		}
		return code;
	}

	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setText("Kim");
			irum.setColumns(10);
			irum.setBounds(81, 43, 109, 23);
		}
		return irum;
	}

	private JTextField getJumin() {
		if (jumin == null) {
			jumin = new JTextField();
			jumin.setText("1234561234567");
			jumin.setColumns(10);
			jumin.setBounds(81, 76, 109, 23);
		}
		return jumin;
	}

	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setText("Seoul");
			address.setColumns(10);
			address.setBounds(81, 109, 109, 23);
		}
		return address;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("연  락  처");
			label_5.setFont(new Font("굴림", Font.PLAIN, 13));
			label_5.setBounds(224, 10, 66, 23);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("메일주소");
			label_6.setFont(new Font("굴림", Font.PLAIN, 13));
			label_6.setBounds(226, 43, 81, 23);
		}
		return label_6;
	}

	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("부       서");
			label_7.setFont(new Font("굴림", Font.PLAIN, 13));
			label_7.setBounds(226, 76, 81, 23);
		}
		return label_7;
	}

	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("직       책");
			label_8.setFont(new Font("굴림", Font.PLAIN, 13));
			label_8.setBounds(226, 109, 81, 23);
		}
		return label_8;
	}

	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("비밀번호");
			label_9.setFont(new Font("굴림", Font.PLAIN, 13));
			label_9.setBounds(226, 142, 81, 23);
		}
		return label_9;
	}

	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setText("010-9466-7439");
			phone.setColumns(10);
			phone.setBounds(287, 10, 109, 23);
		}
		return phone;
	}

	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setText("Jint@gmail.com");
			email.setColumns(10);
			email.setBounds(287, 43, 109, 23);
		}
		return email;
	}

	private JTextField getPwd() {
		if (pwd == null) {
			pwd = new JTextField();
			pwd.setText("1234");
			pwd.setColumns(10);
			pwd.setBounds(287, 142, 109, 23);
		}
		return pwd;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("등록완료(저장)");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					input();
					
				}
			});
			btnNewButton.setBounds(12, 175, 384, 23);
		}
		return btnNewButton;
	}
	public void input(){
		try{
			
			if (irum.getText().trim().equals("")
					|| jumin.getText().trim().equals("")
					|| address.getText().trim().equals("")
					|| phone.getText().trim().equals("")
					|| email.getText().trim().equals("")

					|| pwd.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "사원정보가 누락 되었습니다");
				code.requestFocus();
				return;// 입력창이 한칸이라도 비었으면 정보누락 메시지 후 텍스트창 비움
			}else{
				
				String c= code.getText();
				String p= pwd.getText();
				String i= irum.getText();
				String j= jumin.getText();
				String pn= phone.getText();
				String e= email.getText();
				String a= address.getText();
				
				String sql ="insert into memberdata(memberid,pwd,irum,iden_number,team,position,phone,email,address,working) "
						+ "values(?,?,?,?,?,?,?,?,?,?)";
				
				
				
				pstmt=
						conn.prepareStatement(sql);
				pstmt.setString(1, c);
				pstmt.setString(2, p);
				pstmt.setString(3, i);
				pstmt.setString(4, j);
				pstmt.setString(5,team2);
				pstmt.setString(6,combo);
				pstmt.setString(7, pn);
				pstmt.setString(8, e);
				pstmt.setString(9, a);
				pstmt.setInt(10, 1);
				pstmt.executeUpdate();//입력,수정,삭제;
				
				JOptionPane.showMessageDialog(null, "사원등록(저장)이 완료 되었습니다");
				memberNum++;
				code.setText("A" + memberNum);
				
				combo = "사원";
				team2 = "재경부";
				jumin.setText("");
				phone.setText("");
				irum.setText("");
				address.setText("");
				email.setText("");

				pwd.setText("");
				code.requestFocus();
				
				//pstmt.executeQuery();//조회
				
				ms.columnadd();
				
			}
			
			
			
			
			
			
			
		}catch(Exception e){
			
		}
	}


	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(287, 109, 109, 23);
			scrollPane.setRowHeaderView(getComboBox_1());
		}
		return scrollPane;
	}

	private JComboBox getComboBox_1() {
		if (position == null) {
			position = new JComboBox(str);
			position.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == e.SELECTED) {
						combo = (String) (e.getItem());
					}

				}
			});

			position.setPreferredSize(new Dimension(107, 21));

		}
		return position;
	}

	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(287, 77, 109, 22);
			scrollPane_1.setRowHeaderView(getTeam());
		}
		return scrollPane_1;
	}

	private JComboBox getTeam() {
		if (team == null) {
			team = new JComboBox(str2);
			team.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ex) {
					if (ex.getStateChange() == ex.SELECTED) {
						team2 = (String) (ex.getItem());
					}

				}
			});
			team.setPreferredSize(new Dimension(107, 21));
		}
		return team;
	}
}
